package com.lanou.controller;
import com.lanou.bean.AdminInfo;
import com.lanou.service.AdminServiceImpl;

import jdk.nashorn.internal.ir.annotations.Ignore;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.util.List;


/**
 * Created by lanouhn on 17/10/17.
 */
@Controller
public class AdminController{

    @Resource
    private AdminServiceImpl adminService;

    @RequestMapping("/getAdmin")
    public String login(AdminInfo adminInfo, String codenum, HttpSession session, HttpServletResponse response) {

//       获取查询的返回值
        AdminInfo adminInfo1 = adminService.selectAdmin(adminInfo);
           if(adminInfo1==null){
               session.setAttribute("loginmsg", "用户名或者密码错误");
           }
//        获取当前的登录状态
        AdminInfo adminInfo2 = (AdminInfo) session.getAttribute("adminInfo");

//        接收验证码
        String code = (String) session.getAttribute("checkcode");
        System.out.println(code + "验证码");
        if (adminInfo2 == null) {
//            service的验证码为空时
            if (org.springframework.util.StringUtils.isEmpty(code) || code.equalsIgnoreCase(codenum)) {
                if (adminInfo != null) {
                    Cookie cookie = new Cookie("autoLogin", adminInfo.getName() + "-" + adminInfo.getPassword());
                    cookie.setMaxAge(60);
                    cookie.setPath("/");
                    response.addCookie(cookie);
                    session.setAttribute("adminInfo", adminInfo1);
                    System.out.println(cookie.getName());
                    return "index";
                }
            } else {
                if (!code.equalsIgnoreCase(codenum)) {
                    session.setAttribute("loginmsg", "验证码错误");
                    return "login";
                }
            }
        }
        return "login";
    }

//    查询所有的用户
    @RequestMapping("/selectAllAdmin")
    public String selectAllAdmin(HttpSession session){
//        获取所有的管理员信息
      List<AdminInfo> adminInfoList= adminService.selectAllAdmin();
        System.out.println(adminInfoList);
      session.setAttribute("adminList",adminInfoList);

        //        获取所有角色信息，放到session中
        List<String> rolelist=adminService.selectRoleList();
        System.out.println(rolelist);
        session.setAttribute("rolelist",rolelist);

      return "admin/admin_list";

    }

//    插入用户
    @RequestMapping("/insertAdminRole")
    public String insertAdminRole(String admin_code,String name,String password,
                                  String telephone,String email,String module,HttpSession session
                                  ){

//          显示数据
        System.out.println(admin_code+name+password+telephone+email+module);
//        插入数据
        adminService.insertAdmin(admin_code,name,password,telephone,email);
//        获取role_id
        int role_id=  adminService.selectIdByname(admin_code);

 //        将前端的module截取,获取对应的module中id
        String[] modulelist = module.split(",");

//        创建一个数组，存储权限的id
        int[] moduleid = new int[modulelist.length];

//            遍历数组
        for (int i = 0; i < modulelist.length; i++) {
            System.out.println(modulelist[i]);
            moduleid[i] = adminService.getId(modulelist[i]);
            System.out.println(moduleid[i]);
        }
//         roel_module表插入关联
        System.out.println("获取到的权限个数:" + moduleid.length);
        for (int i = 0; i < moduleid.length; i++) {
            adminService.insertAdminRole(role_id,moduleid[i]);
        }
       return "admin/admin_list";
    }


//    删除管理员信息
    @RequestMapping("/deleteAdminRoleInfo")
    public String deleteAdminRoleInfo(Integer admin_id,HttpSession session){
        System.out.println(admin_id);

        int result2= adminService.deleteAdminroleInfo(admin_id);
        System.out.println(result2);

        List<Integer> role_idlist=adminService.getRoleId(admin_id);
        int[] idlist=new int[role_idlist.size()];
        for (int i = 0; i <role_idlist.size() ; i++) {
            System.out.println(role_idlist);
         idlist[i]=adminService.deleteAdminroleInfo(role_idlist.get(i));
        }

        int result1= adminService.deleteAdmin(admin_id);

        if (result1!=0&&result2!=0){
            session.setAttribute("mag","删除成功");

        }else {
            session.setAttribute("msg","删除失败，请重新删除");
        }

        return "admin/admin_list";
    }


    //管理员信息回显
    @RequestMapping("/selectAdminInfoById")
    public String selectAdminInfoById(Integer admin_id,HttpSession session){
        AdminInfo adminInfo=adminService.selectAdminInfoById(admin_id);
        System.out.println(adminInfo+"信息会显");
        session.setAttribute("adminInfo",adminInfo);
        return "admin/admin_modi";
    }
//    管理员信息修改后保存
    @RequestMapping("/updateAdmin")
    public String updateAdmin(HttpSession session,Integer beforeId,String beforeName,String admin_code,String name,
    String telephone,String email,String module){
        System.out.println(beforeId+beforeName+admin_code+name+telephone+email+module);
//      更新操作
      int result=adminService.updateAdminInfoById(beforeId,admin_code,name,telephone,email);
        System.out.println(result);

//      删除中间表的关联主键
       int result2= adminService.deleteAdminroleInfo(beforeId);
        List<Integer> role_idlist=adminService.getRoleId(beforeId);
        int[] idlist=new int[role_idlist.size()];
        for (int i = 0; i <role_idlist.size() ; i++) {
            System.out.println(role_idlist);
            idlist[i]=adminService.deleteAdminroleInfo(role_idlist.get(i));
        }

        // 将前端的module截取,获取对应的module中id
        String[] modulelist = module.split(",");
//        创建一个数组，存储权限的id
        int[] moduleid = new int[modulelist.length];

//            遍历数组
        for (int i = 0; i < modulelist.length; i++) {
            System.out.println(modulelist[i]);
            moduleid[i] = adminService.getId(modulelist[i]);
            System.out.println(moduleid[i]);
        }
//         roel_module表插入关联
        System.out.println("获取到的权限个数:" + moduleid.length);

        for (int i = 0; i < moduleid.length; i++) {
           adminService.insertAdminRole(beforeId,moduleid[i]);
        }

        return "admin/admin_list";
    }

//    更新信息并保存
    @RequestMapping("/updateSave")
    public String updateSave(Integer admin_id,String name,String telephone,String email){
        int result=adminService.updateSave(admin_id,name,telephone,email);
        System.out.println(result+"更新修改成功");
        return "admin/admin_list";
    }


//    修改密码
    @RequestMapping("/updatepassword")
    public String updatepassword(Integer admin_id,String password){
        int result=adminService.updatepassword(admin_id,password);
        System.out.println(result+"修改密码成功");
        return "admin/admin_list";
    }

    public AdminServiceImpl getAdminService() {
        return adminService;
    }

    public void setAdminService(AdminServiceImpl adminService) {
        this.adminService = adminService;
    }

}
